//
//  InfoDetail.m
//  Trading365
//
//  Created by Jason on 12-2-3.
//  Copyright (c) 2012年 dhc. All rights reserved.
//

#import "CustomerInfo.h"
#import "InfoType.h"
#import "TaskManager.h"
#import "XmlRpc.h"
#import "InfoImageCell.h"
#import "CustomerInfoDetail.h"
#import "CustomerContactList.h"
#import "CustomerPictList.h"
#import "ImageLoader.h"
#import "HttpServerURL.h"


@implementation CustomerInfo
@synthesize table;
@synthesize key_name;
@synthesize key_id;
@synthesize InfoListTitle,InfoListName;
@synthesize responseParam;

@synthesize employeeName;

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title=self.key_name;
    [self.table setBackgroundColor:BACKGROUND_COLOR];

    NSArray *title_array = [[NSArray alloc] initWithObjects:@"产品图片",@"客户全称",@"客户类型",
                            @"信用等级",@"更多",nil];
    self.InfoListTitle = title_array; 
    [title_array release];
    indexMore=[self.InfoListTitle count]-1;

    NSArray *name_title = [[NSArray alloc] initWithObjects:@"default_image_id",@"customer_name_cn",@"customer_type_name",
                           @"creadit_level",@"",nil];
    self.InfoListName = name_title; 
    [name_title release];

    [self refreshData];
    [self.table reloadData];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    [[[TaskManager Share] GetTaskByType:GetProductInfo Delegate:self] cancel];
}

- (void)dealloc {
    [responseParam release];
    
    self.key_name=nil;
    self.key_id=nil;
    self.InfoListTitle=nil;
    self.InfoListName = nil;
    
    [table release];
    [super dealloc];
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0&&indexPath.row == 0) {
        return 60;
    }
    else
        return 44;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (section == 0) {
        return [self.InfoListTitle count];
    }
    else
        return 1;
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    
    if (indexPath.section == 0){
        NSUInteger row = [indexPath row];
        if ([responseParam count] < 1)
            return cell;
        
        if (row==0) {
            static NSString *CellIdentifier1 = @"InfoImageCell";
            
            InfoImageCell* cell1 = (InfoImageCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
            if(cell1 == nil){
                NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"InfoImageCell" owner:self options:nil];
                cell1 = [nib objectAtIndex:0];
            }

            cell1.name.text = @"客户图片";
            cell1.picture.image = [UIImage imageNamed:PIC_INFO_DEFAULT_PEOPLE];  
            NSString *default_image_id=[responseParam objectForKey:@"default_image_id"];
            if ([default_image_id length] > 0){
                NSString *imageURL =[[NSString alloc]initWithFormat:@"%@%@%@",[HttpServerURL GetInstance].serverImgUrl,default_image_id,[HttpServerURL GetInstance].serverIconUrlExt];
                [[ImageLoader sharedLoader] setImageWithURL:imageURL toView:cell1.picture];
                [imageURL release];
            }
            
            cell1.contentView.backgroundColor = [UIColor clearColor];
            cell1.backgroundColor = [UIColor clearColor];
            cell1.backgroundView.backgroundColor = [UIColor clearColor];

            return cell1;

        }
  
        cell.imageView.image = nil;
        
        if (row < [self.InfoListTitle count]) {
            cell.textLabel.text = [self.InfoListTitle objectAtIndex:row];
        }
        if (row < [self.InfoListName count]) {
            cell.detailTextLabel.text =[responseParam objectForKey:[self.InfoListName objectAtIndex:row]];
        }
        
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        if (row == indexMore){
            cell.detailTextLabel.text=@"";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    
    if (indexPath.section == 1){
        cell.textLabel.text=@"联系人列表";
        cell.detailTextLabel.text=@"";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    if (indexPath.section == 2){
        cell.textLabel.text=@"客户图片列表";
        cell.detailTextLabel.text=@"";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
        
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.  
    
    if (indexPath.section == 0 && indexPath.row < indexMore){
        return;
    }

    if (indexPath.section == 0 && indexPath.row == indexMore){
        CustomerInfoDetail *info = [[CustomerInfoDetail alloc] init];
        
        if ([[responseParam objectForKey:@"is_send_mail_limit"] isEqualToString:@"0"]){
            [responseParam setValue:@"不审批" forKey:@"is_send_mail_limit"];
        }else{
            [responseParam setValue:@"审批" forKey:@"is_send_mail_limit"];
        }
        [info setInfoDetail:responseParam];
        [self.navigationController pushViewController:info animated:YES];
        [info release];
    }
    //@"联系人列表";
    if (indexPath.section == 1){
        CustomerContactList * contactList=[[CustomerContactList alloc] init];
        [contactList setCustomer_id:self.key_id];
        [self.navigationController pushViewController:contactList animated:YES];
        [contactList release];
    }
    //@"图片列表";
    if (indexPath.section == 2){
        CustomerPictList * pictList=[[CustomerPictList alloc] init];
//        [pictList setCustomerID:[responseParam objectForKey:@"id"]];
        [pictList setCustomerID:self.key_id];
        [self.navigationController pushViewController:pictList animated:YES];
        [pictList release];
    }
}

#pragma mark - 
#pragma mark - my func

-(void) OnPreExcute
{
    [super OnPreExcute];
}

-(void) DoInBackground
{
    XmlRpc *xml = [[XmlRpc alloc] init];
    NSMutableDictionary *tmp;

    tmp = [xml GetCustomerInfo:self.key_id];
    
    responseParam = [[NSMutableDictionary alloc] initWithDictionary:tmp];
    
    [self.table reloadData];
    [xml release];
}

-(void) OnPostExcute
{
    [super OnPostExcute];
}

-(void)refreshData
{    
    [[[TaskManager Share] GetTaskByType:GetCustomerInfoList Delegate:self] Excute];
}

@end
